﻿using AutoMapper;
using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.ProductionWorkOrders;
using MangoMes.Commont.DTOs.CommontFiles;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.ProductionWorkOrders
{
    public class ProductionOrdersHander : IRequestHandler<ProductionOrdersCommand, Resqults>
    {
        private readonly IBateRepository<ProductionOrders> procdotion;
        private readonly IMapper mapper;

        public ProductionOrdersHander(IBateRepository<ProductionOrders> procdotion, IMapper mapper)
        {
            this.procdotion = procdotion;
            this.mapper = mapper;
        }

        public async Task<Resqults> Handle(ProductionOrdersCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults res = new Resqults() { Code = 0, Messges = "失败" };
                ProductionOrders resoInfo=mapper.Map<ProductionOrders>(request);
                DapperHelper dapperHelper = new DapperHelper();
                string sql = "";
                switch (resoInfo.Pid)
                {
                    case 0:
                         sql = "insert into productionworkorders values(@ProductionWorkOrdersID,@ProductionWorkOrdersNo,@ProductionWorkOrdersName,@Source,@TicketsType,@OrderNumber,@ProductsNo,@TicketsNum,@Remark,@State,@DemandDate,@BatchNo,@QrCore,@CustomerNo)";
                        res.Code = await procdotion.Add(sql, resoInfo, dapperHelper.MangoMesWrite);
                        if (res.Code > 0)
                        {
                            res.Messges = "成功";
                        }
                        break;
                    case 1:
                         sql = @"UPDATE productionworkorders set ProductionWorkOrdersNo=@ProductionWorkOrdersNo,ProductionWorkOrdersName=@ProductionWorkOrdersName,
                                 Source=@Source,TicketsType=@TicketsType,OrderNumber=@OrderNumber,ProductsNo=@ProductsNo,
                                 TicketsNum=@TicketsNum,Remark='@Remark',State=@State,DemandDate=@DemandDate,BatchNo=@BatchNo,QrCore=@QrCore,CustomerNo=@CustomerNo WHERE ProductionWorkOrdersNo=@ProductionWorkOrdersNo";
                        res.Code = await procdotion.Add(sql, resoInfo, dapperHelper.MangoMesWrite);
                        if (res.Code > 0)
                        {
                            res.Messges = "成功";
                        }
                        break;
                }
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
